package com.mou.controller.user;

import com.mou.constant.MessageConstant;
import com.mou.constant.UserConstant;
import com.mou.context.BaseContext;
import com.mou.dto.*;
import com.mou.entity.Product;
import com.mou.entity.User;
import com.mou.exception.CategoryErrorException;
import com.mou.result.Result;
import com.mou.service.ProductService;
import com.mou.service.UserService;
import com.mou.vo.ProductDetailVO;
import com.mou.vo.ProductVO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;

@RestController("userProductController")
@RequestMapping("/user/product")
@Slf4j
public class ProductController {
    @Autowired
    private ProductService productService;

    @GetMapping("/list")
    public Result<List<ProductVO>> list() {
        log.info("获取用户界面所有产品");
        List<ProductVO> productVOList = productService.getList();
        return Result.success(productVOList);
    }

    @PostMapping("/search")
    public Result<List<ProductVO>> search(@RequestBody ProductSearchDTO productSearchDTO) {
        log.info("搜索产品:{}", productSearchDTO);
        List<ProductVO> productVOList = productService.search(productSearchDTO);
        return Result.success(productVOList);
    }

    @PostMapping("/detail")
    public Result<ProductDetailVO> getProductById(@RequestBody ProductDetailDTO productDetailDTO) {
        log.info("获取产品详情:{}", productDetailDTO);
        ProductDetailVO productDetailVO = productService.getDetailById(productDetailDTO);
        return Result.success(productDetailVO);
    }

    @PostMapping("/addProduct")
    public Result<String> addProduct(@RequestBody ProductUserDTO productUserDTO) {
        productService.add(productUserDTO);
        return Result.success(MessageConstant.ADD_PRODUCT_SUCCESS);
    }

    @PostMapping("/getMyProduct/{id}")
    public Result<List<ProductVO>> getMyProduct(@PathVariable("id") Integer id) {
        log.info("获取用户产品");
        List<ProductVO> productVOList = productService.getMyProduct(id);
        return Result.success(productVOList);
    }
    @PutMapping("/update")
    public Result<String> update(@RequestBody ProductDTO productDTO) {
        log.info("商品修改:{}", productDTO);
        Product product = new Product();
        BeanUtils.copyProperties(productDTO, product);
        productService.updateById(product);
        return Result.success("商品修改成功");
    }
    @DeleteMapping("/delete/{id}")
    public Result<String> delete(@PathVariable("id") List<Integer> id) {
        log.info("删除商品:{}", id);
        productService.removeByIds(id);
        return Result.success("删除成功");
    }

}
